<html>
<head>
	<title>BlackJack</title>

	<link rel="stylesheet" type="text/css" href="game.css">



		<?php 
		include('connect.php');
		
		session_start();
		
		$sessionID = $_SESSION['SessionID'];		
		
		// Usernamen aus Datenbank lesen
		$anfrage = "SELECT Username from tblusers WHERE ID LIKE '$sessionID' LIMIT 1";
		$ergebnis = mysql_query($anfrage);
		$userName = mysql_result($ergebnis, 0); /** mysql_result ist set PHP 5.5.0 veraltet und sollte ausgetauscht werden **/
		

	// Derzeitiges Guthaben des Spielers aus der Datenbank lesen
		$anfrage = "SELECT Cash from tblusers WHERE ID LIKE '$sessionID' LIMIT 1";
		$ergebnis = mysql_query($anfrage);
		$cash = mysql_result($ergebnis, 0); /** mysql_result ist seit PHP 5.5.0 veraltet und sollte ausgetauscht werden **/
		
		// Derzeitiges Guthaben des Spielers aus der Datenbank lesen
		$anfrage = "SELECT Level from tblusers WHERE ID LIKE '$sessionID' LIMIT 1";
		$ergebnis = mysql_query($anfrage);
		$level = mysql_result($ergebnis, 0); /** mysql_result ist seit PHP 5.5.0 veraltet und sollte ausgetauscht werden **/
	?>



	<script type = "text/javascript">
	var cashPlayer1='<?php echo $cash;?>';    //<-  Guthaben des Spielers
	var winPlayer1;
	var gameOver; 
	var cardCount;
	var doubled=0;
	var betPlayer1=0;
	var handPlayer1=0;
	var handDealer=0;
	var cardImageCounterPlayer1=0;
	var cardImageCounterDealer=0;
	var firstBet;

	setInterval("console.log(doubled);", 2000);

	function Shuffle(max){
		var num=Math.random()*max;

		return Math.round(num)+1;
	}
	function getSuit(){
		suit = Shuffle(4);
		if(suit == 1) return "S";
		if(suit == 2) return "C";
		if(suit == 3) return "D";
		else return "H";
	}
	function cardName(card){
		if(card == 1) return "1";
		if(card == 11) return "J";
		if(card == 12) return "Q";
		if(card == 13) return "K";
		return "" + card;
	}
	function cardValue(card,strWho){
		if(card == 1) {
			if(strWho =="You" && document.display.you.value >10){
				return 1;}
				else return 11; }
				if(card > 10) return 10; 
				return card;
			}
			function PickACard(strWho){
				card = Shuffle(12);
				suit = getSuit();
				if(strWho =="You")
				{

					cardImageCounterPlayer1++;
					var currentImgID='cardspace2'+cardImageCounterPlayer1;
					//console.log(currentImgID);
					document.getElementById(currentImgID).src=("cards/"+suit+cardName(card)+".gif");

					changeValue("info","");
				}
				else{
					cardImageCounterDealer++;
					var currentImgIdDealer='cardspace1'+cardImageCounterDealer;
					document.getElementById(currentImgIdDealer).src=("cards/"+suit+cardName(card)+".gif");
					changeValue("info","");
				}


				return cardValue(card,strWho);
			}
			function NewHand(form){
				betPlayer1=form.bet.value;
				document.getElementById("bet").disabled=true;
				if(gameOver !=0)
					{ 
					changeValue("info","Hand in Play! or Bet is to high");
					return;
					}
				else
					{
						
						if(betPlayer1<=cashPlayer1*1+form.win.value*2 && betPlayer1!=0){
						form.dealer.value = 0; form.you.value = 0; cardCount=0;
						
						handPlayer1=0;
						handDealer=0;

						cardImageCounterPlayer1=0;
						cardImageCounterDealer=0;

						for (var i = 1; i <= 6; i++){
							var cardSetDealer='cardspace1'+i;
							var cardSetPlayer='cardspace2'+i;
							document.getElementById(cardSetDealer).src="cards/empty.png";
							document.getElementById(cardSetPlayer).src="cards/empty.png";

						}
						form.dealer.value = eval(form.dealer.value) + PickACard("Dealer");
						form.you.value = eval(form.you.value) + PickACard("You");
						gameOver= -1; cardCount+=1;

					console.log(form.bet.value);
					betPlayer1=form.bet.value;
					}
					else
						{changeValue("info","Invalid Bet!");
							document.getElementById("bet").disabled=false;
				}
				}
			}
					function Dealer(form){
						if (gameOver ==0)
						{

							changeValue("info","Deal the Cards!");
							return;}
							else
								if(form.you.value<10)
								{

									changeValue("info","Not Below Ten!, Take a Hit!");
									return;}
									else
										if (cardCount <2)
										{

											changeValue("info","Minimum 2 Cards!, Hit Again!");
											return;}
											else
												while(form.dealer.value < 17)
													{form.dealer.value = eval(form.dealer.value) + PickACard("Dealer");}
											}
											function User(form){
												if (gameOver ==0)
												{


													changeValue("info","Deal the Cards!");
													return;}
													else
														{cardCount+=1; 


															form.you.value = eval(form.you.value) + PickACard("You");}
															if(form.you.value > 21)
															{

																changeValue("info","You Busted!");
																loseCalculation(form);


															}
															}
											function LookAtHands(form){

																if (gameOver ==0 || form.you.value<10 || cardCount <2){return;}
																else
																	if(form.dealer.value > 21)
																	{

																		changeValue("info","House Busts!, You Win!");

																		gameOver=0; 
																		form.numgames.value=eval(form.numgames.value)+1; 
																		//cashPlayer1=cashPlayer1*1+betPlayer1*1; 
																		winPlayer1=betPlayer1;
																		
																		form.win.value=betPlayer1;
																		//changeValue("win",betPlayer1);
																		//changeValue("cash",cashPlayer1); 
																		riskDisabled(false);
																		//document.getElementById("deal").disabled=true;
																		dealDisabled(true);
																		takeDisabled(false);
																	}
																		else
																			if(form.you.value > form.dealer.value)
																			{
																				
																				changeValue("info","You Win!");
																				gameOver=0; 
																				form.numgames.value=eval(form.numgames.value)+1; 
																				//cashPlayer1=cashPlayer1*1+betPlayer1*1; 
																				winPlayer1=betPlayer1;
																				
																				form.win.value=betPlayer1;
																			//changeValue("win",betPlayer1);
																				//changeValue("cash",cashPlayer1); 
																				riskDisabled(false);
																				takeDisabled(false);
																				//document.getElementById("deal").disabled=true;

																				dealDisabled(true);
																			}
																				else
																					if(form.dealer.value == form.you.value)
																					{	

																						changeValue("info","Game Tied!, Try Again");

																						loseCalculation(form);
																					}
																						else
																						{	
																							changeValue("info","House Wins!, Next Time!");
																							loseCalculation(form);
																						}
																						}



															function setBj(){
																gameOver=0; cardCount=0; 
																document.display.dealer.value=""; 
																document.display.you.value="";
																document.display.numgames.value="0";

																changeValue("info","Press 'Deal' to Start");
																//cashPlayer1=100;
																changeValue("cash",cashPlayer1);
																document.getElementById("bet").max=cashPlayer1;


																			}
																						
															function changeValue(id,value){
																document.getElementById(id).firstChild.data=value;
																						  }
															
															function risk(){
																firstBet=betPlayer1;
																betPlayer1= betPlayer1*2;
																display.bet.value=betPlayer1;
																NewHand(display);
																document.getElementById("deal").disabled;
																doubled=doubled+1;
																
																document.getElementById("bet").disabled=true;
																showSliderValue(document.getElementById("range").innerHTML);

															}
															function take()
															{
																cashPlayer1=cashPlayer1*1+betPlayer1*1; // <---- Guthaben wird hinzugefügt
																updateDbCash();
																changeValue("cash",cashPlayer1); // <---- Guthaben wird angezeigt
																resetMaxBet();
																display.win.value=0;
																//changeValue("win"," ");
																dealDisabled(false);
																riskDisabled(true);
																takeDisabled(true);
																doubled=0;
																document.getElementById("bet").disabled=false;
																
															}
															function loseCalculation(form)
															{
																gameOver=0;
																form.numgames.value=eval(form.numgames.value)-1;
																							
																if (doubled==0){								// wenn noch nie verdoppelt wurde
																		cashPlayer1=cashPlayer1*1-betPlayer1*1;

																				}
																else{
																  
																  		cashPlayer1=cashPlayer1*1-firstBet*1;  //<-- Guthaben wird um den ersten einsatz reduziert

																  	}
																updateDbCash(); // <--- Guthaben in der Datenbank wird geändert
																changeValue("cash",cashPlayer1);
																resetMaxBet();				
																riskDisabled(true); 
																dealDisabled(false);
																takeDisabled(true);
																winPlayer1=0;
																form.win.value=0;
																document.getElementById("bet").disabled=false;
																							//changeValue("win"," ");
																doubled=0;
																betPlayer1=0;
																form.bet.value=0;
																showSliderValue(0);
															}
															function riskDisabled(status)
															{
																document.getElementById("riskbutton").disabled=status;
															}
															function dealDisabled(status)
															{
																document.getElementById("deal").disabled=status;
															}
															function takeDisabled(status)
															{
																document.getElementById("takebutton").disabled=status;
															}
															function showSliderValue(newValue)
															{	

																document.getElementById("range").innerHTML=newValue;
															}
															function resetMaxBet()
															{
																document.getElementById("bet").max=cashPlayer1;
															}
															function updateDbCash()
 															{ 	
																var id = parseInt("<?php echo $sessionID?>", 10);
																if (window.XMLHttpRequest) {
																	xmlhttp=new XMLHttpRequest(); // Für IE7+, Chrome, Firefox, etc.
																}
																else { 
																	xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); // Für IE5/6
																}
																xmlhttp.open("GET","updateCash.php?cash="+cashPlayer1+"&id="+id,true);
																xmlhttp.send();
															}





																						</script>
																					</head>
																					
																					<body OnLoad="setBj()">
																					
																						<div id="content">
																							<div id="logo"><img src="logo.png"></div>
																							
																							<form name="display">
																								

																									<div id="gamesWon">
																									Games won:
																									<input type=text name="numgames" size="3" value="0" disabled>
																								</div>


																											<div id="betDiv">
																											<table border="0" cellspacing="0" cellpadding="3">
																											<tr><td>Cash:</td><td id="cash" name="cash"> </td></tr>
																											<tr><td>Win:</td><td><input type=text id="win" name="win" disabled size="2"> </td></tr>
																											<tr><td>Bet:</td><td><input type=range name="bet" id="bet" size="2" max="1" onchange="showSliderValue(this.value)" ><span id="range">1</span></td></tr>
																											<tr><td>																											
																											<input type=button value="Deal" class="buttonGame" onClick="NewHand(this.form)" id="deal" >
																											<input type=button value="Risk" class="buttonGame" onClick="risk()" disabled id="riskbutton">
																											<input type=button value="Take" class="buttonGame" onClick="take()" disabled id="takebutton">

																										</td></tr>
																									</table>
																								</div>
																								

																								<div id="info" name="info"> </div>
																								<div id="table"><img src="table.png">
																								<div id="dealerCards">
                                                                                                        <table border="0" cellspacing="0" cellpadding="3">    																					
                                                                                                        <td>Dealer:</td>
																										<td><input type=text name="dealer" size="2" disabled></td>							 
                                                                                                        <div id="playerCardsImg"><td><img src="cards/empty.png" id="cardspace11" name="cardspace11"></td>
																										<td><img src="cards/empty.png" id="cardspace12" name="cardspace12"></td>
											 															<td><img src="cards/empty.png" id="cardspace13" name="cardspace13"></td>
																										<td><img src="cards/empty.png" id="cardspace14" name="cardspace14"></td>
																										<td><img src="cards/empty.png" id="cardspace15" name="cardspace15"></td>
																										<td><img src="cards/empty.png" id="cardspace16" name="cardspace16"></td></div>
                                                                                                        </table>																											
                                                                                                 </div>
                                                                                                 <div id="playerCards">           
                                                                                                         <table border="0" cellspacing="0" cellpadding="3">             
                                                                                                         
                                                                                                        <td>Player:</td>
																										<td><input type=text name="you" size="2" disabled></td>
                                                                                                        <td><img src="cards/empty.png" id="cardspace21" name="cardspace21"></td>
																										<td><img src="cards/empty.png" id="cardspace22" name="cardspace22"></td>
																										<td><img src="cards/empty.png" id="cardspace23" name="cardspace23"></td>
																										<td><img src="cards/empty.png" id="cardspace24" name="cardspace24"></td>
																										<td><img src="cards/empty.png" id="cardspace25" name="cardspace25"></td>
																										<td><img src="cards/empty.png" id="cardspace26" name="cardspace26"></td>
																										</table>
																								</div>	
																								</div>		
																											
																											<div id="playButtons"><table>
																												<tr><td>
																											<input type=button value="Stand" class="buttonExtra" onClick="Dealer(this.form);LookAtHands(this.form);"></td>
																											<td><input type=button value=" Hit " class="buttonExtra" onClick="User(this.form)"></td></tr>
																											</table>
																										</div>


                                                                                                <input type=button value="Back to Profile" class="buttonGame" onClick="window.location='profile.php'" id="backButton">
																									</form>
																									
																						</div>
																								</body>
																								</html>